<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/2 0002
 * Time: 9:22
 */

namespace App\Base\Middleware;

use App\Base\Exceptions\ApiException;
use App\Weixin\Models\WeixinUserModel;
use Illuminate\Http\Request;
use Closure;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

/**
 * 入驻会员中间件
 * Class ApiIntoPermissionMiddleware
 * @package App\Base\Middleware
 */
class ApiIntoPermissionMiddleware
{



    /**
    * @return WeixinUserModel
     */
   public function getWeixinUserModel(){
        return app()->make(WeixinUserModel::class);
   }
    /**
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
//        $token=$request->input("token");
//        if(empty($token)){
//            throw  new ApiException(ApiException::ERROR_AUTHORIZATION);
//        }
//
//        $key=WeixinUserModel::CACHE_KEY.$token;
//        //获取用户id
//        $uid=Cache::get($key);
//        if(empty($uid)){
//            throw  new ApiException(ApiException::ERROR_AUTHORIZATION);
//        }
        define("AUID", 16);  //记住密码登陆
        //define("AUID",15);  define("AUID",$uid);
        $userInfo=$this->getWeixinUserModel()->newQuery()->where("uid",AUID)->first();
        if($userInfo->lid!=2){
            throw  new ApiException(ApiException::ERROR_ACCESS_NOT_ENABLE);  //拒绝访问
        }
        define("LID", $userInfo->lid);  //用户层级
        if($userInfo->status==0){
            throw  new ApiException(ApiException::ERROR_ACCESS_NOT_ENABLE);  //被冻结
        }


//        $this->userInfo=$userInfo;
        $response = $next($request);
        return $response;
    }
}